package hot100;

import java.util.*;

public class Lc49 {
    public List<List<String>> groupAnagrams(String[] strs) {
        Map<String, List<String>> map = new HashMap<>();
        for (String str : strs) {
            String[] chars = str.split("");
            List<String> list = new ArrayList<>(Arrays.stream(chars).sorted().toList());
            list.sort(String::compareTo);
            StringBuilder key = new StringBuilder();
            for (String c : list) {
                key.append(c);
            }
            if (!map.containsKey(key.toString())) {
                map.put(key.toString(), new ArrayList<>());
            }
            map.get(key.toString()).add(str);
        }
        return new ArrayList<>(map.values());
    }
}
